package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ExaminationInfo;
import com.ruoyi.system.mapper.ExaminationInfoMapper;
import com.ruoyi.system.service.IExaminationInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@RequiredArgsConstructor
@Service
public class ExaminationInfoServiceImpl implements IExaminationInfoService {

    private final ExaminationInfoMapper baseMapper;

    @Override
    public TableDataInfo<ExaminationInfo> selectPageExaminationInfoList(ExaminationInfo examinationInfo, PageQuery pageQuery) {
        Page<ExaminationInfo> page =  baseMapper.selectPage(pageQuery.build(), this.buildQueryWrapper(examinationInfo));
        return TableDataInfo.build(page);
    }

    @Override
    public List<ExaminationInfo> selectExaminationInfoList(ExaminationInfo examinationInfo) {
        return baseMapper.selectList(this.buildQueryWrapper(examinationInfo));
    }

    private LambdaQueryWrapper<ExaminationInfo> buildQueryWrapper(ExaminationInfo examinationInfo) {
        LambdaQueryWrapper<ExaminationInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(examinationInfo.getUserName()), ExaminationInfo::getUserName, examinationInfo.getUserName());
        lqw.eq(StringUtils.isNotBlank(examinationInfo.getIdCard()), ExaminationInfo::getIdCard, examinationInfo.getIdCard());
        lqw.eq(StringUtils.isNotBlank(examinationInfo.getPhoneNumber()), ExaminationInfo::getPhoneNumber, examinationInfo.getPhoneNumber());
        return lqw;
    }

    @Override
    public ExaminationInfo selectExaminationInfoById(Long testId) {
        return baseMapper.selectById(testId);
    }

    @Override
    public int insertExaminationInfo(ExaminationInfo examinationInfo) {
        return baseMapper.insert(examinationInfo);
    }

    @Override
    public int updateExaminationInfo(ExaminationInfo examinationInfo) {
        return baseMapper.updateById(examinationInfo);
    }

    @Override
    public int deleteExaminationInfoByIds(Long[] testIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(testIds));
    }
}
